package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.liusy.jdbc.JdbcHelp;

public class CustomerGroup {
	
	
	public static void main(String[] args) {
		Comparator c = new Comparator<Map<String,Object>>(){
		@Override
		public int compare(Map<String, Object> arg0, Map<String, Object> arg1) {
			
			if( Float.parseFloat(arg0.get("kcLoad").toString())<Float.parseFloat(arg1.get("kcLoad").toString()))
				return 1;
			else if( Float.parseFloat(arg0.get("kcLoad").toString())>Float.parseFloat(arg1.get("kcLoad").toString()))
				return -1;
			else if(Float.parseFloat(arg0.get("kcLoad").toString())==Float.parseFloat(arg1.get("kcLoad").toString()))
			{
				if(String.valueOf(arg0.get("lineId")).lastIndexOf(",")<String.valueOf(arg1.get("lineId")).lastIndexOf(","))
					return 1;
				if(String.valueOf(arg0.get("lineId")).lastIndexOf(",")>String.valueOf(arg1.get("lineId")).lastIndexOf(","))
					return -1;
			}
			return 0;
			
		}};

			  java.util.Date current=new java.util.Date();
	    java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		System.out.println(sdf.format(current));

		
		
		JdbcHelp jdbcHelp=  JdbcHelp.getJdbcHelp();
		List<Map<String,Object>> list = jdbcHelp.getDataObject(
				"select smt.zone_id,\n" +
						"       smt.container_id line_Id,\n" + 
						"       wm_concat(smt.consumer_id) consumer_id,\n" + 
						"       (sum(DCML.YEARMAXLOAD) - sum(DCML.YEARMINLOAD)) kc_load\n" + 
						"  from (select sst.id, sst.container_id, sst.zone_id, sst.consumer_id\n" + 
						"          from SM_SPECIALTY_TRANSFORMER sst\n" + 
						"        union\n" + 
						"        select spt.id, spt.container_id, spt.zone_id, spt.consumer_id\n" + 
						"          from SM_PUBLIC_TRANSFORMER spt) smt,\n" + 
						"       dsm_customer_median_load DCML\n" + 
						" where smt.id = DCML.Transformer_Id(+) and DCML.YEARMAXLOAD is not null\n" + 
						" group by smt.zone_id, smt.container_id\n" + 
						" order by smt.zone_id, kc_load desc\n" + 
						""
 , jdbcHelp.getConnection("CustomerGroup"));
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		Object[] Obj06110 = null;
		Map<String,Object> map=null;
		Map<String, Object> map1=null;
		float kcLoad1=0;
		float kcLoad2=0;
		String lineId1="";
		String lineId2="";
		int flag=0;
		String zoneId="";
		String consumerId1 = "";
		String consumerId2 = "";
		String [] group = new String[]{"A组","B组","C组","D组","E组","F组","G组"};
		//第一步，每个所下面根据可错负荷平均分7组
			for (int i = 0; i < list.size(); i++) {
				map1 =list.get(i);
				if(!map1.get("zoneId").equals(zoneId))
				{
					flag=0;
					zoneId = (String) map1.get("zoneId");
					if(null!=Obj06110)
					listMap.addAll( (Collection<? extends Map<String, Object>>) Arrays.asList(Obj06110));
					Obj06110 = new Object[7];
				}
				if(flag<7)
				{
					map1.put("group", group[flag]);
					Obj06110[flag++] = map1;
					list.remove(map1);
				}
				else
				{
					Arrays.sort(Obj06110,c);
					map = (Map<String, Object>) Obj06110[6];
					kcLoad1 =Float.parseFloat(String.valueOf(map.get("kcLoad")));
					kcLoad2 =Float.parseFloat(String.valueOf(list.get(i).get("kcLoad")));
					lineId1 =(String)map.get("lineId");
					lineId2 =(String) list.get(i).get("lineId");
					
					consumerId1 =(String)map.get("consumerId");
					consumerId2 =(String) list.get(i).get("consumerId");
					
					map.put("lineId", lineId1+","+lineId2);
					map.put("kcLoad", String.valueOf(kcLoad1+kcLoad2));
					map.put("consumerId", String.valueOf(consumerId1+","+consumerId2));
				}
				
				
			}
			System.out.println(listMap.size());
			
			//第二步 所有的所根据可错负荷合并为7组，每次在同一个所只能取一次
		
			List<Map<String,Object>>  listMap1= new ArrayList<Map<String,Object>>();
			Obj06110 = new Object[7];
			for (int i = 0; i < listMap.size(); i++) {
				map1 =listMap.get(i);
				if(i<7)
				{
				Obj06110[i] = map1;
				listMap.remove(map1);
				}
				else
				{
					if(null!=map1)
						listMap1.add(map1);
				}
			}
		
			Object [] obj =listMap1.toArray();
			Arrays.sort(obj,c);
			listMap1.clear();
			listMap1.addAll( (Collection<? extends Map<String, Object>>) Arrays.asList(obj));
			while(listMap1.size()>7)
			{
				for (int i = 0; i < 7; i++) {
					Arrays.sort(Obj06110,c);
					for (int j = 0; j < listMap1.size(); j++) {
						map1 =(Map<String, Object>) listMap1.get(j);
						
						if(!map1.get("zoneId").equals(zoneId))
						{
							zoneId = (String) map1.get("zoneId");
							map = (Map<String, Object>) Obj06110[6];
							kcLoad1 =Float.parseFloat(String.valueOf(map.get("kcLoad")));
							kcLoad2 =Float.parseFloat(String.valueOf(map1.get("kcLoad")));
							lineId1 =(String)map.get("lineId");
							lineId2 =(String) map1.get("lineId");
							
							consumerId1 =(String)map.get("consumerId");
							consumerId2 =(String) map1.get("consumerId");
							
							map.put("lineId", lineId1+","+lineId2);
							map.put("kcLoad", String.valueOf(kcLoad1+kcLoad2));
							map.put("consumerId", String.valueOf(consumerId1+","+consumerId2));
							
							Arrays.sort(Obj06110,c);
							
							listMap1.remove(map1);
							break;
						}
					
					}
				}
			}
			//第三步，将最后几组按大小分配
			for (int j = 0; j < 7; j++) {
				try {
					map1 =(Map<String, Object>) listMap1.get(0);
				} catch (IndexOutOfBoundsException e) {
					break;
				}
					zoneId = (String) map1.get("zoneId");
					map = (Map<String, Object>) Obj06110[6];
					kcLoad1 =Float.parseFloat(String.valueOf(map.get("kcLoad")));
					kcLoad2 =Float.parseFloat(String.valueOf(map1.get("kcLoad")));
					lineId1 =(String)map.get("lineId");
					lineId2 =(String) map1.get("lineId");
					
					consumerId1 =(String)map.get("consumerId");
					consumerId2 =(String) map1.get("consumerId");
					
					map.put("lineId", lineId1+","+lineId2);
					map.put("kcLoad", String.valueOf(kcLoad1+kcLoad2));
					map.put("consumerId", String.valueOf(consumerId1+","+consumerId2));
					
					Arrays.sort(Obj06110,c);
					
					listMap1.remove(map1);
			
			}
			System.out.println(Obj06110.length);
			
	}
}
